﻿using Nova_BLL;
using Nova_Common.Extend;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.RFSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.RFSystem.Controllers
{
    /// <summary>
    /// 手持ERP物料组盘功能
    /// </summary>
    [RoutePrefix("api/RFSystem/Package")]
    [ControllerGroup("PDA手持", "ERP物料组盘功能接口")]
    public class PackageController : BaseApiController
    {
        RFBll RfBll = new RFBll();

        /// <summary>
        ///  获取托盘组盘的物料信息
        /// </summary>
        /// <param name="trayId">托盘编码</param>
        /// <returns></returns>
        [HttpGet, Route("GetTrayMaterialList")]
        public ApiResult GetTrayMaterialList(string trayId)
        {
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘编号不能为空!");
            DataTable dt = RfBll.GetTrayMaterialInfor(trayId);
            List<Tray_MaterialInfoModel> list = Common.DataTableToList<Tray_MaterialInfoModel>(dt);
            return Success("获取托盘组盘的物料信息!", list);
        }

        /// <summary>
        ///  获取托盘组盘的物料信息
        /// </summary>
        /// <param name="trayId">托盘编码</param>
        /// <returns></returns>
        [HttpGet, Route("GetTrayMaterialListV")]
        public ApiResult GetTrayMaterialListV(string trayId)
        {
            TRelationsBll bll = new TRelationsBll();
            if (trayId.IsEmpty())
                return Error(ResultCode.ParaError, "托盘编号不能为空!");
            DataTable dt = RfBll.GetTrayMaterialInfor(trayId);
            List<Tray_MaterialInfoModel> list = Common.DataTableToList<Tray_MaterialInfoModel>(dt);
            var relationEntity = bll.GetEntityByTrayId(trayId);
            int typeOfSize = relationEntity != null ? relationEntity.TypeOfSize : 0;
            return Success("获取托盘组盘的物料信息!",
                    new { TrayId = trayId, TypeOfSize = typeOfSize, List = list });
        }

        /// <summary>
        /// 获取未处理入库单据列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("GetStorageList")]
        public ApiResult GetStorageList()
        {
            DataTable dt = RfBll.DataListStorage();
            List<StorageInfoModel> list = Common.DataTableToList<StorageInfoModel>(dt);
            return Success("获取未处理入库单据列表!", list);
        }

        /// <summary>
        /// 根据入库单获取物料信息
        /// </summary>
        /// <param name="stockNo">入库单据</param>
        /// <returns></returns>
        [HttpGet, Route("GetMaterialList")]
        public ApiResult GetMaterialList(string stockNo)
        {
            if (stockNo.IsEmpty())
                return Error(ResultCode.ParaError, "stockNo不能为空!");
            var dt = RfBll.DataListMaterial(stockNo);
            List<MaterialInfoModel> list = Common.DataTableToList<MaterialInfoModel>(dt);
            return Success("根据入库单获取物料信息!", list);
        }

        /// <summary>
        /// 根据入库单和物料编码获取物料数量
        /// </summary>
        /// <param name="stockNo"></param>
        /// <param name="secMatId"></param>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet, Route("GetAddStorageMaterialInfo")]
        public ApiResult GetAddStorageMaterialInfo(string stockNo, int secMatId,string pid)
        {
            if (stockNo.IsEmpty())
                return Error(ResultCode.ParaError, "stockNo不能为空!");
            if (secMatId <= 0)
                return Error(ResultCode.ParaError, "secMatId不能为空!");
            pid = string.IsNullOrEmpty(pid) ? "" : pid;
            DataTable dt = RfBll.GetAddStorageMaterialInfo(stockNo, secMatId,pid);
            List<Tray_AddStorageMaterialModel> list = Common.DataTableToList<Tray_AddStorageMaterialModel>(dt);
            return Success(list.FirstOrDefault());
        }


        /// <summary>
        /// 判断托盘是否可以组盘
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost, Route("IsSetPackage")]
        public ApiResult IsSetPackage(SetPackageModel param)
        {
            if (param.StrStockNo.IsEmpty())
                return Error(ResultCode.ParaError, "StrStockNo不能为空!");
            if (param.StrTrayId.IsEmpty())
                return Error(ResultCode.ParaError, "StrTrayId不能为空!");
            if (param.MaterialCode.IsEmpty())
                return Error(ResultCode.ParaError, "MaterialCode不能为空!");
            if (param.Size <= 0)
                return Error(ResultCode.ParaError, "Size不能为空!");
            if (param.MatNum <= 0)
                return Error(ResultCode.ParaError, "MatNum要大于0!");
            DataTable dt = RfBll.TrayCanPackage(param.StrTrayId);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString() != "0")
                {
                    return Error(ResultCode.Error, "该托盘已经在立体库，不允许再次组盘！");
                }
            }
            return Success();
        }

        /// <summary>
        /// 根据入库单据组盘-绑定托盘和物料
        /// </summary>
        /// <param name="param">组盘参数</param>
        /// <returns></returns>
        [HttpPost, Route("SetPackage")]
        public ApiResult SetPackage(SetPackageModel param)
        {
            StoreProceResult res = RfBll.SetPackage(param.StrStockNo, param.StrTrayId, param.MaterialCode, param.SecMatId, param.MatNum, param.Size);
            if (res.ResNo == 1)
                return Success("组盘成功!");
            else
                return Error(ResultCode.Error, res.ResMsg);
        }

        /// <summary>
        /// 解盘-解绑托盘和物料的关系
        /// </summary>
        /// <param name="param">解盘参数</param>
        /// <returns></returns>
        [HttpPost, Route("PackageDel")]
        public ApiResult PackageDel(SetPackageModel param)
        {
            string message = string.Empty;
            DataTable dt = RfBll.TrayCanPackage(param.StrTrayId);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString() != "0")
                {
                    return Error(ResultCode.Error, "该托盘已经在立体库，不允许解盘！");
                }
            }
            StoreProceResult res = RfBll.PackageDel(param.StrStockNo,param.StrTrayId,param.SecMatId,param.MatNum);
            if (res.ResNo== 1)
                return Success("解绑成功!");
            else
                return Error(ResultCode.OtherError, res.ResMsg);
        }
    }
}
